<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once(RGL_FS_WEB_ROOT.'/app/_inc/inc.session.php');
require_once(RGL_FS_WEB_ROOT.'/app/_inc/funct.html.php');

reqDef('lingua','en');
$lingua = reqVar('lingua');
$aLingue["en"] = "english";
$aLingue["de"] = "deutsch";
$aLingue["fr"] = "francaise";

if (reqVar('azione') == 'salva') {
	$msgPag = salva();
}

$aItal = $aTrad = array();
if (file_exists(RGL_TRADUZIONI_DIR."/testi_$lingua.txt") 
&& $fh=fopen(RGL_TRADUZIONI_DIR."/testi_$lingua.txt",'rb')) {
	while($sRec = rtrim(fgets($fh,1024))) {
		$aX = explode("",$sRec);
		$kX = md5($aX[0]);
		$aItal[$kX] = $aX[0];
		$aTrad[$kX] = $aX[1];
	}
	fclose($fh);
}
$sSql = "SELECT * FROM rgl_testi";
$objDB->query($sSql);
while($acX = $objDB->fetch()) {
	$kX = md5($acX['testo_it']);
	if (!isset($aItal[$kX])) {
		$aItal[$kX] = $acX['testo_it'];
		//dbg_("new:".$kX.":".$acX['testo_it']);
	}
}


if (reqVarTrim('pagifilt')!='') {
	$pagiFilt = reqVarTrim('pagifilt');
	foreach($aItal as $kX => $testoIt) {
		if (stripos($testoIt,$pagiFilt)===false) {
			unset($aItal[$kX]);
			continue;
		}
	}
}
if (reqVar('daTrad')=='S') {
	foreach($aItal as $kX => $testoIt) {
		if (rglVx($aTrad,$kX)!='') {
			unset($aItal[$kX]);
			continue;
		}
	}
}

asort($aItal);

$numRec = count($aItal);
$link = $_SERVER['PHP_SELF']."?lingua=$lingua&daTrad=".reqVar('daTrad')."&".$sXSNQS;
$startOffset = 0;
$endOffset = 0;
$sFiltro = rglGetFiltro($link);
$sPaginaz = rglGetPaginaz($link, $numRec, $startOffset, $endOffset);

$aOptL = array("onChange"=>"document.frm1.submit();");
$sCmdR  = "";
$sCmdR .= "&nbsp;&nbsp;&nbsp;";
$sCmdR .= "<a class='cmd1' href=\"javascript:document.frm1.azione.value='salva';document.frm1.submit();\" >"
	._t("salva")."</a>";
$sCmdR .=  cmdTorna();

testaHtml('Gestore applicazione', $msgPag, 'gest');
?>
<script type="text/javascript">
function conferma() {
	if (confirm("Confermi  ?")) {
		document.frm1.azione.value='...';
		document.frm1.submit();
	}
}
</script>
<?php 
echo getHtmlForm();
echo getHtmlHidden('azione','...');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('startoffset');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left" valign='bottom'>
         <? 
         $aOptL = array("onChange"=>"document.frm1.submit();");
         echo _t("Lingua").":&nbsp;".getHtmlSelect('lingua',$aLingue,"@REQ",$aOptL);?>
      </td>
      <td class="cmd1" align="right" valign='bottom'>
         <? echo $sCmdR;?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left">
			<? 
			echo $sFiltro;
			echo "&nbsp;&nbsp;&nbsp;&nbsp;";
			$aOptDT = array("onClick"=>"document.frm1.startoffset.value='0';document.frm1.submit();");
			echo getHtmlCheckBox('daTrad', 'S', '@REQ', $aOptDT)."&nbsp;"._t("solo da tradurre");
			?>
			
      </td>
      <td class="cmd1" align="right">
			<?=$sPaginaz?>
      </td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<tr>
		<td class="tit1" colspan='2' align='center'><?=_t("Traduzioni")." italiano - ".$aLingue[$lingua]?></td>
	</tr>
	<tr>
		<td class="lab1" width='50%' align='center'>
			Italiano (&lt;blank&gt; = <?=_t("cancella")?>)
		</td>
		<td class="lab1" width='50%' align='center'>
			English (&lt;blank&gt; = <?=_t("non traduce")?>)
		</td>
	</tr>
<?php 
$kk=0;
foreach($aItal as $kX => $testoIt) {
	if ($kk < $startOffset) {
		$kk++;
		continue;
	}
	if ($kk >= $endOffset) {
		break;
	}
	echo getHtmlHidden("testo_kx$kk",$kX);
?>
	<tr>
		<td class="txt1" width='50%'>
			<input type='text' style='width:100%;' name="testo_it<?=$kk?>" class='inp1' value="<?=htmlentities($testoIt)?>"/>
		</td>
		<td class="txt1" width='50%'>
			<input type='text' style='width:100%;' name="testo_tr<?=$kk?>" class='inp1' value="<?=htmlentities(rglVx($aTrad,$kX))?>"/>
		</td>
	</tr>
<?php 
	$kk++;
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			&nbsp;
      </td>
      <td class="cmd1" align="right">
			<?=$sPaginaz?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left" valign='bottom'>
         &nbsp;
      </td>
      <td class="cmd1" align="right" valign='bottom'>
         <? echo $sCmdR;?>
      </td>
   </tr>
</table>
<?
echo "<form>\n";
codaHtml();
exit();

function salva() {
	global $objDB;
	$lingua = reqVar('lingua');
	$aItal = $aTrad = array();
	if (file_exists(RGL_TRADUZIONI_DIR."/testi_$lingua.txt") 
	&& $fh=fopen(RGL_TRADUZIONI_DIR."/testi_$lingua.txt",'rb')) {
		while($sRec = fgets($fh,1024)) {
			$aX = explode("#@",$sRec);
			$kX = md5($aX[0]);
			$aItal[$kX] = $aX[0];
			$aTrad[$kX] = substr($aX[1],0,-2);
		}
		fclose($fh);
	}
	foreach($_REQUEST as $nX => $md5X) {
		if (substr($nX,0,8)!='testo_kx') continue;
		$kk = substr($nX,8);
		/*//////
		if (md5(reqVar("testo_it$kk")) != $md5X) { //cambia testo italiano su DB
			$sSql = "DELETE FROM rgl_testi WHERE md5x = '$md5X'";
			$objDB->exec($sSql);
			//dbg_($sSql);
			$sSql = "REPLACE rgl_testi SET md5x = '".md5(reqVar("testo_it$kk"))."'"
				.", testo_it = '".reqVarAS("testo_it$kk")."', time_rec = NOW()";
			$objDB->exec($sSql);			
		}
		//*////
		if (reqVarTrim("testo_it$kk")=='') {
			$sSql = "DELETE FROM rgl_testi WHERE md5x = '$md5X'";
			$objDB->exec($sSql);
			//dbg_($sSql.":".reqVar("testo_it$kk").":".$objDB->affectedRec());
			unset($aItal[$md5X]);
			continue;
		}
		if (reqVarTrim("testo_tr$kk")=='') {
			//dbg_("unset:$md5X:".reqVar("testo_it$kk").":".count($aItal));
			unset($aItal[$md5X]);
			//dbg_("unsetted:".count($aItal));
			continue;
		}
		$aItal[$md5X] = reqVar("testo_it$kk");
		$aTrad[$md5X] = reqVar("testo_tr$kk");
		//dbg_("caricato");
	}
	$fh = fopen(RGL_TRADUZIONI_DIR."/testi_$lingua.txt",'wb');
	foreach($aItal as $kX => $testoIt) {	
		$sRec = $testoIt."#@".$aTrad[$kX]."\r\n";
		//dbg_($testoIt."#@".$aTrad[$kX]);
		fwrite($fh,$sRec);
	}
	fclose($fh);
	return _t("salvato");
}

